<?php

namespace App\Http\Controllers\Api;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\FieldTable;
use App\Model\LockSetTable;
use App\Model\PriceSetTable;
use App\Model\PriceSetTempTable;
use App\Model\StorefrontTable;
use App\Model\TimeBlockTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Common\Fun;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

//场地管理
class FieldController extends ApiController
{


    /**
     * @api {get} /api/field/list 获取场地列表
     * @apiGroup Api-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_id  店铺id 可选
     * @apiParam {Int} type  场地类型  可选
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "8",
     * "list": [
     * {
     * "id": "11",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "10",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "6",
     * "type": "6",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "6V6"
     * },
     * {
     * "id": "5",
     * "type": "6",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "6V6"
     * },
     * {
     * "id": "4",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "3",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "2",
     * "type": "5",
     * "storefront_id": "1",
     * "number": "2",
     * "type_text": "5V5"
     * },
     * {
     * "id": "1",
     * "type": "1",
     * "storefront_id": "1",
     * "number": "1",
     * "type_text": "1V1"
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getList()
    {
        $store_id = \Request::get('store_id');  //店面
        $type = \Request::get('type');  //场地类型
        return JsonRespose::done(FieldTable::getList(1, 1000, $store_id, $type));
    }

    /**
     * @api {get} /api/field/time-list 获取比赛时段
     * @apiGroup Api-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_id --
     * @apiParam {Int} type 场地类型
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "start_time": "09:00",
     * "end_time": "11:00"
     * },
     * {
     * "start_time": "11:00",
     * "end_time": "13:00"
     * },
     * {
     * "start_time": "13:00",
     * "end_time": "15:00"
     * },
     * {
     * "start_time": "15:00",
     * "end_time": "17:00"
     * },
     * {
     * "start_time": "17:00",
     * "end_time": "19:00"
     * },
     * {
     * "start_time": "19:00",
     * "end_time": "21:00"
     * },
     * {
     * "start_time": "21:00",
     * "end_time": "23:00"
     * }
     * ]
     * }
     *
     */
    public function getTimeList()
    {

        $store_id = \Request::get('store_id');  //店面
        $type = \Request::get('type');  //场地


        $param = [];
        $where = '1=1';
        if ($type) {
            $where .= ' and price_set.type =:type';
            $param['type'] = $type;
        }
        if ($store_id) {
            $where .= ' and price_set.storefront_id =:storefront_id';
            $param['storefront_id'] = $store_id;
        }

        //所有的配置
        $res = \DB::select("select price_set.*,field.id as field_id from price_set LEFT  JOIN field ON  
(field.storefront_id=price_set.storefront_id and field.type=price_set.type and field.program=price_set.program  )   
WHERE {$where} GROUP BY price_set.id ORDER BY price_set.start_time asc ,price_set.`type` ASC   ", $param);

        $count1=false;
        $count2=false;
        foreach ($res as $v) {
            if(!$v->field_id){
                continue;  //影藏没有对应场地的价格设置
            }
            if($v->program==2){
                $count2=1;
            }
            if($v->program==1){
                $count1=1;
            }
        }

        $key = 0;
        if ($count1 and $count2) {
            $key = 3;
        } elseif ($count1) {
            $key = 1;
        } elseif ($count2) {
            $key = 2;
        }


        if(!$key){
            $time_list=[];
        }else{
            $time_list = TimeBlockTable::getTimeList($key);
        }

        

        $data = [];
        foreach ($time_list as $v) {
            $v['start_time'] = date('H:i', $v['start_time']);
            $v['end_time'] = date('H:i', $v['end_time']);
            $data[] = $v;
        }
        return JsonRespose::success($data);
    }


    /**
     * @api {get} /api/field/info-list 订场选择场地
     * @apiGroup Api-Field
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_id  店铺id 可选
     * @apiParam {Int} type  可选
     * @apiParam {Date} day  必选2018-02-02
     * @apiParam {Time} time  11:00,起始时间
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * {
     * "storefront_name": "汉阳",
     * "type_text": "1V1",
     * "number": "1",
     * "list": [
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "09:00",
     * "end_time": "11:00",
     * "status": 1
     * },
     * {
     * "lock": 0,
     * "price": "300.00",
     * "order_id": 0,
     * "start_time": "11:00",
     * "end_time": "13:00",
     * "status": 1
     * },
     */
    public function getInfoList()
    {
        $day = \Request::get('day');  //日期 必选
        $store_id = \Request::get('store_id');  //店面
        $time = \Request::get('start_time');  //字符串时间
        $type = \Request::get('type');  //场地

        if (!$day) {
            return JsonRespose::fail('失败');
        }


        $db = FieldTable::ORM();

        if ($store_id) {
            $db->where('storefront_id', $store_id);
        }
        if ($type) {
            $db->where('type', $type);
        }

        $field_list = $db->orderBy('number','asc')->get();
        $list = [];

        $day = Str::getDayTime($day);

        foreach ($field_list as $field) {
            $info = FieldTable::getFieldInfo($field->id);

            $time_list = TimeBlockTable::getFieldTimeList($field->id, $day);
            if ($time) {
                $time_list[] = Fun::getTimeSection(Str::timeTotime($time));
            }
            foreach ($time_list as $v) {
                $data = TimeBlockTable::getBlockStatus($day, $field->id, $v['start_time'], $v['end_time']);
                //$data['status']=FieldTable::getFieldStatus($data['order_id'],$data['lock']);
                $data['field_id'] = $field->id;
                if ($data['status'] != 1) {
                    //continue;  //显示已预定的
                }
                $list[$info['storefront_name'] . '_' . $info['type_text'] . '_' . $info['number'] . '_' . $info['field_type']][] = $data;
            }
        }

        $data = [];
        foreach ($list as $k => $v) {
            $tmp = explode('_', $k);
            $data[] = [
                'storefront_name' => $tmp[0],
                'type_text' => $tmp[1],
                'number' => $tmp[2],
                'type' => $tmp[3],
                'list' => $v
            ];
        }
        return JsonRespose::success($data);
    }

}